﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public DataSet QueryLIEFDAT(DataRow dwData, int option = 1)
        {
            DataSet setReturn = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            OracleCommand or1 = OracleDB.CreateCommand();
            // APZMANDANT  APZART APZVAR APZANRKRS APZALFDNR APZSTUFE
            switch (option)
            {
                case 1:
                    or1.CommandText = "SELECT * FROM LIEFDAT WHERE  "
                    + "  LSMANDANT=:LSMANDANT AND LSLIEFNR=:LSLIEFNR";
                    or1.Parameters.Add("LSMANDANT", OracleType.NVarChar).Value = dwData["LSMANDANT"].ToString();
                    or1.Parameters.Add("LSLIEFNR", OracleType.NVarChar).Value = dwData["LSLIEFNR"].ToString();
                    break;
                case 2:
                    or1.CommandText = "SELECT * FROM LIEFDAT WHERE  "
                    + "  LSMANDANT=:LSMANDANT AND LSLIEFNR=:LSLIEFNR";
                    or1.Parameters.Add("LSMANDANT", OracleType.NVarChar).Value = dwData["LSMANDANT"].ToString();
                    or1.Parameters.Add("LSLIEFNR", OracleType.NVarChar).Value = dwData["LSLIEFNR"].ToString();
                    break;
                case 3:
                    or1.CommandText = "SELECT LSLIEFNR,LSLIEFNA,RTRIM(LSLIEFNA)||RTRIM(LSNAME1)  AS LSNAME1  FROM LIEFDAT WHERE  "
                    + "  LSMANDANT=:LSMANDANT AND LSGRSCHL=:LSGRSCHL";
                    or1.Parameters.Add("LSMANDANT", OracleType.NVarChar).Value = dwData["LSMANDANT"].ToString();
                    or1.Parameters.Add("LSGRSCHL", OracleType.NVarChar).Value = dwData["LSGRSCHL"].ToString();
                    break;
                case 4:
                    or1.CommandText = "SELECT DISTINCT LF.* FROM ABFOLGE FL RIGHT JOIN LIEFDAT LF"
                        + " ON FL.ABFMANDANT=LSMANDANT AND FL.ABFLIEFNR=LSLIEFNR"
                        + " WHERE ABFMANDANT=:ABFMANDANT AND ABFAUFKRS=:ABFAUFKRS AND ABFAUFLFD=:ABFAUFLFD";
                    or1.Parameters.Add("ABFMANDANT", OracleType.NVarChar).Value = dwData["ABFMANDANT"].ToString();
                    or1.Parameters.Add("ABFAUFKRS", OracleType.NVarChar).Value = dwData["ABFAUFKRS"].ToString();
                    or1.Parameters.Add("ABFAUFLFD", OracleType.NVarChar).Value = dwData["ABFAUFLFD"].ToString();
                    break;
                case 5:
                    or1.CommandText = "SELECT DISTINCT LF.* FROM EKKOPF FL JOIN LIEFDAT LF"
             + " ON FL.EKKMANDANT=LSMANDANT AND FL.EKKLIEFNR=LSLIEFNR"
             + " WHERE EKKMANDANT=:EKKMANDANT AND EKKAUFKRS=:EKKAUFKRS AND EKKAUFLFD=:EKKAUFLFD";
                    or1.Parameters.Add("EKKMANDANT", OracleType.NVarChar).Value = dwData["EKKMANDANT"].ToString();
                    or1.Parameters.Add("EKKAUFKRS", OracleType.NVarChar).Value = dwData["EKKAUFKRS"].ToString();
                    or1.Parameters.Add("EKKAUFLFD", OracleType.NVarChar).Value = dwData["EKKAUFLFD"].ToString();
                    break;
                case 6:
                    or1.CommandText = "SELECT LSLIEFNR,LSLIEFNA,LSNAME1 FROM LIEFDAT WHERE  "
                    + "  LSMANDANT=:LSMANDANT  ";
                    or1.Parameters.Add("LSMANDANT", OracleType.NVarChar).Value = dwData["LSMANDANT"].ToString();
                    break;
                case 7:
                    or1.CommandText = "SELECT DISTINCT LF.* FROM  LIEFDAT LF"
             + " WHERE LSLIEFNR IN ( SELECT NVL(B.EKALIEFNR2,A.EKKLIEFNR) FROM EKKOPF A,EKADR B "
             + " WHERE A.EKKMANDANT = B.EKAMANDANT(+) AND A.EKKAUFKRS = B.EKAAUFKRS(+) "
             + " AND A.EKKAUFLFD = B.EKAAUFLFD(+) "
             + " AND A.EKKMANDANT=:EKKMANDANT AND A.EKKAUFKRS=:EKKAUFKRS AND A.EKKAUFLFD=:EKKAUFLFD) ";
                    or1.Parameters.Add("EKKMANDANT", OracleType.NVarChar).Value = dwData["EKKMANDANT"].ToString();
                    or1.Parameters.Add("EKKAUFKRS", OracleType.NVarChar).Value = dwData["EKKAUFKRS"].ToString();
                    or1.Parameters.Add("EKKAUFLFD", OracleType.NVarChar).Value = dwData["EKKAUFLFD"].ToString();
                    break;
                default:
                    or1.CommandText = "SELECT * FROM LIEFDAT WHERE  "
                        + "  LSMANDANT=:LSMANDANT AND LSLIEFNR=:LSLIEFNR";
                    or1.Parameters.Add("LSMANDANT", OracleType.NVarChar).Value = dwData["LSMANDANT"].ToString();
                    or1.Parameters.Add("LSLIEFNR", OracleType.NVarChar).Value = dwData["LSLIEFNR"].ToString();
                    break;
            }

            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "LIEFDAT";
            }
            setReturn.Merge(Result);
            return setReturn;
        }
    }
}
